package com.pocketsweet.service;

import android.app.Activity;
import android.content.Context;

import com.pocketsweet.model.MLArticle;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.RenrenSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class shareService {
	private UMSocialService mController;
	private Context ctx;
	private shareCompleteListener mShareCompleteListener;

	public shareService(Context context) {
	}

	public shareService(Context context, MLArticle article) {
		ctx = context;
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		mController.getConfig().setPlatforms(SHARE_MEDIA.SINA,
				SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ,
				SHARE_MEDIA.QZONE, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.RENREN,
				SHARE_MEDIA.TENCENT);
		mController.getConfig().setPlatformOrder(SHARE_MEDIA.SINA,
				SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ,
				SHARE_MEDIA.QZONE, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.RENREN,
				SHARE_MEDIA.TENCENT);

		// 设置微信
		setWeixn(article);
		// 设置qq和qqzone
		setQQ(article);
		// 设置新浪微博
		// setSina();
		// 设置腾讯微博
		setTecent(article);
		// 设置人人
		setRenRen(article);
		// // 设置豆瓣
		// setDouban();
	}

	public void startShare() {
		mController.openShare((Activity) ctx,
				new SocializeListeners.SnsPostListener() {
					@Override
					public void onStart() {
						// Toast.makeText(ctx, "分享开始",
						// Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onComplete(SHARE_MEDIA share_media, int i,
							SocializeEntity socializeEntity) {
//						Log.e("==share",
//								"share complete......." + String.valueOf(i));
						if (i == 200) { // i ＝ 200 即成功
							mShareCompleteListener.onComplate();
						}
					}
				});
	}

	private void setDouban() {

	}

	private void setRenRen(MLArticle article) {
		// 添加人人网SSO授权功能
		// APPID:201874
		// API Key:28401c0964f04a72a14c812d6132fcef
		// Secret:3bf66e42db1e4fa9829b955cc300b737
		RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(ctx, "475020",
				"bc809ccc7db845feadc102dee0d672ae",
				"2e8feeda692b47e9a7bd2941afcc8dd3");
		mController.getConfig().setSsoHandler(renrenSsoHandler);
		// 设置分享内容
		mController.setShareContent(article.getIntro());
		// 设置分享图片, 参数2为图片的url地址
		mController.setShareMedia(new UMImage(ctx, article.getTitleImgUrl()));
		mController.setAppWebSite(
				SHARE_MEDIA.RENREN,
				"http://www.pocketsweet.com/share?articleId="
						+ article.getObjectId());
	}

	private void setTecent(MLArticle article) {
//		Log.e("==share", "start share complete...tencent");

		TencentWbShareContent tencent = new TencentWbShareContent();
		tencent.setShareMedia(new UMImage(ctx, article.getTitleImgUrl()));
		tencent.setTargetUrl("http://www.pocketsweet.com/share?articleId="
				+ article.getObjectId());
		tencent.setShareContent(article.getIntro());
		mController.setShareMedia(tencent);
		mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
	}

	private void setSina() {
		// 设置新浪SSO handler
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
	}

	private UMSocialService setSinaResult() {
		/** 使用SSO授权必须添加如下代码 */
		return mController;
	}

	private void setQQ(MLArticle article) {
		// /////////////////////QQ
		// 参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler((Activity) ctx,
				"1104588970", "JVSuqFVQjNWJJvSI");
		qqSsoHandler.addToSocialSDK();
		QQShareContent qqShareContent = new QQShareContent();
		// 设置分享文字
		qqShareContent.setShareContent(article.getIntro());
		// 设置分享title
		qqShareContent.setTitle("口袋恋人——" + article.getTitle());
		// 设置分享图片
		qqShareContent
				.setShareImage(new UMImage(ctx, article.getTitleImgUrl()));
		// 设置点击分享内容的跳转链接
		qqShareContent
				.setTargetUrl("http://www.pocketsweet.com/share?articleId="
						+ article.getObjectId());
		mController.setShareMedia(qqShareContent);

		// ///////////////////QQZone
		// 参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity) ctx,
				"1104588970", "JVSuqFVQjNWJJvSI");
		qZoneSsoHandler.addToSocialSDK();
		QZoneShareContent qzone = new QZoneShareContent();
		// 设置分享文字
		qzone.setShareContent(article.getIntro());
		// 设置点击消息的跳转URL
		qzone.setTargetUrl("http://www.pocketsweet.com/share?articleId="
				+ article.getObjectId());
		// 设置分享内容的标题
		qzone.setTitle("口袋恋人——" + article.getTitle());
		// 设置分享图片
		qzone.setShareImage(new UMImage(ctx, article.getTitleImgUrl()));
		mController.setShareMedia(qzone);
//		mController.openShare((Activity) ctx, false);
	}

	private void setWeixn(MLArticle article) {
		String appID = "wxaa055b4833fdbf4a";
		String appSecret = "c4f199bd5983ae9309c8bcb4f3bcd0e4";
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(ctx, appID, appSecret);
		wxHandler.addToSocialSDK();
		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(ctx, appID, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

		// 设置微信好友分享内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		// 设置分享文字
		weixinContent.setShareContent(article.getIntro());
		// 设置title
		weixinContent.setTitle("口袋恋人——" + article.getTitle());
		// 设置分享内容跳转URL
		weixinContent
				.setTargetUrl("http://www.pocketsweet.com/share?articleId="
						+ article.getObjectId());
		// 设置分享图片
		weixinContent.setShareImage(new UMImage(ctx, article.getTitleImgUrl()));
		mController.setShareMedia(weixinContent);

		// 设置微信朋友圈分享内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(article.getIntro());
		// 设置朋友圈title
		circleMedia.setTitle("口袋恋人——" + article.getTitle());
		circleMedia.setShareImage(new UMImage(ctx, article.getTitleImgUrl()));
		circleMedia
				.setTargetUrl("http://www.pocketsweet.com/share?articleId="
						+ article.getObjectId());
		mController.setShareMedia(circleMedia);
//		startShare();

//		SnsPostListener mSnsPostListener = new SnsPostListener() {
//
//			@Override
//			public void onStart() {
//
//			}
//
//			@Override
//			public void onComplete(SHARE_MEDIA platform, int stCode,
//					SocializeEntity entity) {
//				Log.e("==share",
//						"share complete......." + String.valueOf(stCode));
//
//				if (stCode == 200) {
//					mShareCompleteListener.onComplate();
//					// Toast.makeText(ctx, "分享成功", Toast.LENGTH_SHORT).show();
//				} else {
//					Toast.makeText(ctx, "分享失败 : error code : " + stCode,
//							Toast.LENGTH_SHORT).show();
//				}
//			}
//		};
//		mController.registerListener(mSnsPostListener);
	}

	public void setCompleteListener(shareCompleteListener l) {
		if (null != l && mShareCompleteListener != l) {
			mShareCompleteListener = l;
		}
	}

	public interface shareCompleteListener {
		public void onComplate();
	}

}
